//
//  UserInfoManager.m
//  DMGME
//
//  Created by ihope99 on 15/9/16.
//  Copyright (c) 2015年 GYP. All rights reserved.
//
#import <Foundation/Foundation.h>

#import "UserInfoManager.h"
static UserInfoManager *gUserManager = nil;

@implementation UserInfoManager

+ (UserInfoManager *)Share {
    if (!gUserManager) {
        gUserManager = [[UserInfoManager alloc] init];
    }
    return gUserManager;
}

//- (NSString *)mStatus {
//    return [self GetUserValueforKey:@"Status"];
//}
//- (void)setMStatus:(NSString *)value {
//    [self SetUserValue:value forKey:@"Status"];
//}

#pragma mark set get


//level

#pragma mark - --  字典里面的东西
- (NSString *)mLevel
{
    return [self GetUserValueforKey:@"level"];
}

- (void)setMLevel:(NSString *)value
{
    [self SetUserValue:value forKey:@"level"];
}
//- (NSString *)mIdentify
//{
//    return [[NSUserDefaults standardUserDefaults] objectForKey:@"level"];
//}
//- (void)setMIdentify:(NSString *)value
//{
//    [[NSUserDefaults standardUserDefaults] setObject:value forKey:@"level"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//}

#pragma mark - --  单独存放
- (NSString *)mUserState
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"userState"];
}
- (void)setMUserState:(NSString *)value
{
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:@"userState"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)mDefaultUser
{
    return (self.mUserDict != nil);
}

- (NSDictionary *)GetFormatDict:(NSDictionary *)dict1
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dict1];
    for (NSString *key in dict.allKeys) {
        id target = [dict objectForKey:key];
        
        if ([target isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:target];
            for (NSString *key2 in dict2.allKeys) {
                id target2 = [dict2 objectForKey:key2];
                if ([target2 isKindOfClass:[NSNull class]]) {
                    [dict removeObjectForKey:key];
                }else if ([target2 isKindOfClass:[NSString class]] && [target2 isEqualToString:@"<null>"]) {
                    [dict removeObjectForKey:key];
                }
            }
        }else{
        
        if ([target isKindOfClass:[NSNull class]]) {
            [dict removeObjectForKey:key];
            
        }else if ([target isKindOfClass:[NSString class]] && [target isEqualToString:@"<null>"]) {
            [dict removeObjectForKey:key];
        }
        }
    }
    return dict;
}

- (NSDictionary *)mUserDict
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"];
}

- (void)setMUserDict:(NSDictionary *)dict
{
    if (dict) {
        dict = [self GetFormatDict:dict];
        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"UserInfo"];
    }else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserInfo"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)SetUserValue:(id)value forKey:(NSString *)key
{
    if (!value || !key) {
        return;
    }
    NSMutableDictionary *userinfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"];
    
    if (userinfo && [userinfo isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *userinfo2 = [NSMutableDictionary dictionaryWithDictionary:userinfo];
        [userinfo2 setObject:value forKey:key];
        
        [[NSUserDefaults standardUserDefaults] setObject:userinfo2 forKey:@"UserInfo"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        NSLog(@"SetUserValue Error:userinfo");
    }
}

- (id)GetUserValueforKey:(NSString *)key
{
    NSDictionary *userinfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserInfo"];
    if (userinfo && [userinfo isKindOfClass:[NSDictionary class]]) {
        return [userinfo objectForKey:key];
    }
    return nil;
}

//- (NSDictionary *)mUserData {
//    return [[NSUserDefaults standardUserDefaults] objectForKey:@"dataDic"];
//}
//- (void)SetUserValue:(NSString *)value forKey:(NSString *)key {
//    if (!value || !key) {
//        return;
//    }
//    NSMutableDictionary *dataDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataDic"];
//    
//    
//    if (dataDic && [dataDic isKindOfClass:[NSDictionary class]]) {
//        NSMutableDictionary *dataDic2 = [NSMutableDictionary dictionaryWithDictionary:dataDic];
//        NSDictionary *userinfo = [dataDic2 objectForKey:@"userinfo"];
//        if ([userinfo respondsToSelector:@selector(setObject:forKey:)]) {
//            NSLog(@"NSMutableDictionary SetUserValue");
//        }
//        else {
//            NSLog(@"NSDictionary SetUserValue");
//        }
//        
//        if (userinfo && [userinfo isKindOfClass:[NSDictionary class]]) {
//            NSMutableDictionary *userinfo2 = [NSMutableDictionary dictionaryWithDictionary:userinfo];
//            [userinfo2 setObject:value forKey:key];
//            
//            [dataDic2 setObject:userinfo2 forKey:@"userinfo"];
//            [[NSUserDefaults standardUserDefaults] setObject:dataDic2 forKey:@"dataDic"];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//        }
//        else {
//            NSLog(@"SetUserValue Error:userinfo");
//        }
//    }
//    else {
//        NSLog(@"SetUserValue Error:dataDic");
//    }
//}
//
//- (NSString *)GetUserValueforKey:(NSString *)key {
//    NSDictionary *dataDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataDic"];
//    
//    if (dataDic && [dataDic isKindOfClass:[NSDictionary class]]) {
//        NSDictionary *userinfo = [dataDic objectForKey:@"userinfo"];
//        if (userinfo && [userinfo isKindOfClass:[NSDictionary class]]) {
//            return [userinfo objectForKey:key];
//        }
//    }
//    return nil;
//}
//
////清空保存数据
//-(void)ClearUserData{
//    NSDictionary *dataDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataDic"];
//    dataDic=nil;
//    [[NSUserDefaults standardUserDefaults]  setObject:dataDic forKey:@"dataDic"];
//    [[NSUserDefaults standardUserDefaults]  synchronize];
//    
//}
@end
